package com.biyao.controller.login;


import com.biyao.schedulingcommon.response.BaseMsg;
import com.biyao.schedulingcommon.response.FailureMsg;
import com.biyao.schedulingcommon.response.StatusCode;
import com.biyao.schedulingcommon.response.SuccessMsg;
import com.biyao.schedulingentity.login.TUserEntity;
import com.biyao.schedulingentity.oauth.TOauthAccessTokenEntity;
import com.biyao.service.model.login.LoginMessage;
import com.biyao.service.model.oauth.OauthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 
 *
 * @author code generator
 * @date 2019-09-16 14:10:02
 */
@Api(tags = {"登录"})
@RestController
@RequestMapping("api/v1/login")
public class TUserController {

    @Autowired
    private LoginMessage loginMessage;

    @Autowired
    private OauthService oauthService;

    @ApiOperation(value = "登录")
    @PostMapping
    public BaseMsg save(@RequestBody TUserEntity tUser) {
         String account=tUser.getAccount();
         String password=tUser.getPassword();
         if(StringUtils.isEmpty(account)||StringUtils.isEmpty(password)){
             return new FailureMsg(StatusCode.BAD_PARAM,"用户名或密码为空");
         }
         TUserEntity tUserEntity= loginMessage.selectIsExistUser(account,password);
         if(tUserEntity!=null){
             TOauthAccessTokenEntity token = oauthService.generateToken(tUserEntity.getId());
             HashMap<String, Object> result = new HashMap<>(16);
             result.put("token", token.getAccessToken());
             return new SuccessMsg(result);
         }

        return new FailureMsg(StatusCode.BAD_PARAM,"用户名或密码不对");
    }



}
